import time

from selenium.webdriver.common.by import By
from selenium.webdriver.common.devtools.v85.log import clear
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from Driver.Driver import Blogdriver

class Bloglogin:
    driver=''
    url=''
    alert=''
    def __init__(self):
        self.url='http://8.137.19.140:9090/blog_login.html'
        self.driver=Blogdriver.driver
        self.driver.get(self.url)


    # 用来测试登陆节省时间的函数
    def Qtest(self,name,password):
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys(name)
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys(password)
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
    def Qdelete(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password"),clear()

    #无空格正确用户名和密码
    def NoSpaceBolgloginSuccess(self):
        self.Qtest("zhangsan","123456")
        assert self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > h3")
        Blogdriver.getscreen()


    #无空格错误用户名和密码
    def NoSpaceBolgloginFalse(self):
        self.Qtest("zzz",123456)
        self.alert=WebDriverWait(self.driver,2).until(expected_conditions.alert_is_present())
        self.alert.accept()
        self.Qdelete()

        self.Qtest("zhangsan", 123)
        self.alert = WebDriverWait(self.driver, 2).until(expected_conditions.alert_is_present())
        self.alert.accept()
        self.Qdelete()

        self.Qtest("zzz",126)
        self.alert = WebDriverWait(self.driver, 2).until(expected_conditions.alert_is_present())
        self.alert.accept()
        Blogdriver.getscreen()
        self.Qdelete()

    #含非必要空格的账户名
    def SpaceInFront(self):
        self.Qtest(" zhangsan",123456)
        self.alert=WebDriverWait(self.driver,2).until(expected_conditions.alert_is_present())
        self.alert.accept()
        Blogdriver.getscreen()
        self.Qdelete()
    def SpaceInBack(self):
        self.Qtest("zhangsan ", 123456)
        self.alert = WebDriverWait(self.driver, 2).until(expected_conditions.alert_is_present())
        self.alert.accept()
        Blogdriver.getscreen()
        self.Qdelete()

    #登陆界面博客列表
    # 点击主页
    def titlebarHome(self):
        self.driver.find_element(By.CSS_SELECTOR,"body > div.nav > a:nth-child(4)").click()
        Blogdriver.getscreen()
    # 点击编辑页
    def titlebarCreate(self):
        self.driver.find_element(By.CSS_SELECTOR,"body > div.nav > a:nth-child(5)").click()
        Blogdriver.getscreen()
